﻿namespace _031委托_Lambda表达式_LINQ串讲_1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Student stduent = new Student();
            //委托是一种类类型

            //声明一个委托 
            //MyDele dele1 = new MyDele(() =>
            //{
            //    Console.WriteLine("Hello");
            //});

            //MyDele dele2 = new MyDele(M1);
            //dele2 += M1;//多播委托
            //dele2 += stduent.SayHello;
            //dele2 += (new Student()).SayHello;
            //dele2.Invoke();

            //MyDele dele = new MyDele(Add);
            //int res = dele.Invoke(100, 200);
            //Console.WriteLine(res);

            MyDele<int> deleAdd = new MyDele<int>(Add);
            int res = deleAdd(100, 200);
            Console.WriteLine(res);

            MyDele<double> deleMul = new MyDele<double>(Mul);
            var result =deleMul.Invoke(1.0,2.0);
            Console.WriteLine(result);

            Action<string, string> action = new Action<string, string>(SayHello);
            action.Invoke("lili","chengzi");

            var  action2 =new Action<string, string,int>(SayHello);

            action2.Invoke("lili", "chengzi",6);

            var act = (string a, string b, int round) =>
            {
                SayHello(a, b, round);
            };



            //act("lili", "chengzi",1314);

            //原始委托
            deleTec deletec = new deleTec(SayHello);

            deletec("lili", "chengzi", 6);

        }

        static int Add(int a, int b)
        {
            return a + b;
        }

        static void SayHello(string name1, string name2) {
            Console.WriteLine($"Hello,{name1} and {name2}");
        }

        static void SayHello(string name1, string name2,int round)
        {
            for (int i = 0; i < round;i++) {
               Console.WriteLine($"Hello,{name1} and {name2}");   
            }
              
        }

        static double Mul(double x, double y)
        {
            return x * y;
        }
    }

    //声明一个MyDele的委托
    //delegate int MyDele(int a, int b);

    //泛型委托  
    delegate T MyDele<T>(T a, T b);
    delegate T MyDeleTec<T>(T a, T b,T c);
    delegate void deleTec(string a, string b, int c);

    class Student
    {
        public void SayHello()
        {

            Console.WriteLine("Hello,I'm a Stduent");
        }
    }
}
